package team8.helpers;


import java.util.LinkedList;
import team8.database.databaseconnector;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class user {
	
	private timehelper timer= new timehelper();
	private databaseconnector database;
	
	public user (databaseconnector db)
	{
		database = db;
	}
	/*
	public user (String name, String weight, String age)
	{
		Preferences prefs = Preferences.userNodeForPackage(user.class);
		
		prefs.put("name", name);		
		prefs.put("distance", "0.00");
		prefs.put("weight", weight);
		prefs.put("age", age);
		prefs.put("time", "0"); //in seconds
	}*/
		
	/*public LinkedList<String> getuserdata ()
	{
		LinkedList<String> res = new LinkedList<String>();
		
		Preferences prefs = Preferences.userNodeForPackage(user.class);
		
		res.add(prefs.get("name", ""));
		res.add(prefs.get("distance", "0.00"));
		res.add(prefs.get("weight", "0"));
		res.add(prefs.get("age", "0"));
		
		
		int s= Integer.valueOf(prefs.get("time", "0"));
		int hours = s/3600;
		int minutes = ((s-(hours*3600))/60);
		int seconds = (s-(hours*3600)-(minutes*60));
		
		
		if (hours<10)
			res.add("0"+hours+":"+minutes+":"+seconds);
		else
			res.add(hours+":"+minutes+":"+seconds);
		
		
		
		return res;
		
	}*/
	
	/*public void storeuserdata(String name,double distance, int weight, int age, String time )
	{
		
		String filename="userdata";
		FileOutputStream fos = openFileOutput(filename,Context.MODE_PRIVATE );
		openFileOutput();
		fos.write(string.getBytes());
		fos.close();
		
		Preferences prefs = Preferences.userNodeForPackage(user.class);
		if(name!="")
			prefs.put("name", name);
		
		prefs.put("distance", String.valueOf(Double.valueOf(prefs.get("distance", "0")+distance)));
		
		
		if (weight!=0)
			prefs.put("weight", String.valueOf(weight));
		if (age!=0)
			prefs.put("age", String.valueOf(age));
		
		if (time!=null)
		{
			int currenttime=timer.converttimeseconds(time);
			prefs.put("time", String.valueOf(Integer.valueOf(prefs.get("time", "0")+currenttime)));
		}
		
		
		
	}*/
	
	public LinkedList<String> getuserdata ()
	{
		LinkedList<String> res = new LinkedList<String>();
		
		SQLiteDatabase db = database.getReadableDatabase();
	    Cursor cursor = db.query(databaseconnector.USER, null, null, null, null,
	        null, null);
	    
	    //startManagingCursor(cursor);
	    
	    while (cursor.moveToNext()) {
	      long id = cursor.getLong(0);
	      String name = cursor.getString(1);
	      int age = cursor.getInt(2);
	      int weight = cursor.getInt(3);
	      double distance = (double)cursor.getFloat(4);
	      int time = cursor.getInt(5); //in seconds
	     
	      res.add(name);
	      res.add(String.valueOf(numberformat.round2commas(distance)));	      	      
	      res.add(String.valueOf(weight));
	      res.add(String.valueOf(age));
	      
	      //calc time
		  res.add(timer.secondstotime(time));
		  
		  //calc speed
		  double hours2 =((double)time)/3600.00;		
		  double speed =numberformat.round2commas(distance/hours2);
		  
		  res.add(String.valueOf(speed));
	    }
	    cursor.close();
	    return res;
	}
	
	public void storeuserdata(String name,double distance, int weight, int age, String time )
	{
		SQLiteDatabase db = database.getWritableDatabase();
	    Cursor cursor = db.query(databaseconnector.USER, null, null, null, null,
	        null, null);
	    
	    int test =0;
	    // test if there exists already a user
	   // startManagingCursor(cursor);
	    while (cursor.moveToNext())
	    {test++;}
	    
	    if (test>0)
	    {
	    	LinkedList<String> res=getuserdata();
	    	ContentValues values = new ContentValues();
		    values.put("name", name); 
		    values.put("age", age);
		    values.put("weight", weight);
		    values.put("distance", numberformat.round2commas(distance+Double.valueOf(res.get(1))));
		    values.put("time", timer.converttimeseconds(time)+Integer.valueOf(timer.converttimeseconds(res.get(4))));
	    	
		    db.update(databaseconnector.USER, values, "_id= 1", null);
	    }
	    else
	    {	    				
	    	ContentValues values = new ContentValues();
		    values.put("name", name); 
		    values.put("age", age);
		    values.put("weight", weight);
		    values.put("distance", numberformat.round2commas(distance));
		    values.put("time", timer.converttimeseconds(time));
		    
		    db.insert(databaseconnector.USER, null, values);
	    }
	    cursor.close();
	}
}
